package
{
	/**
	 *
	 *@author CrazyChen 
	 *2013-4-10
	 */	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class PlantView extends Sprite
	{
		private var _isCanClick:Boolean = true;
		
		private var ary:Array;
		
		private var isDelete:Boolean;
		public function PlantView()
		{
			super();
			ary = [];
			this.addEventListener(MouseEvent.CLICK,editingPlant);
		}
		
		public function printPantData(e:MouseEvent):void{
			var xml:XML = <plants>
										
										  </plants>;
			var plantAry:Array = ary.concat();
			var plant:PlantItem;
			while(plantAry.length){
				plant = plantAry.shift();
				var xxx:XML = <elements cls='' x='' y='' width='' height='' hp=''  energy='' gradlist=''/>;
				xxx.@x = plant.x;
				xxx.@y = plant.y;
				xxx.@width = plant.width;
			    xxx.@height = plant.height;
				xxx.@hp = plant.hp;
				xxx.@energy = plant.energy;
				var str:String = "";
				var p:Point;
				for(var i:int=0;i<plant.gradAry.length;i++){
					p = plant.gradAry[i];
					str+=p.x+":"+p.y+",";
				}
				xxx.@gradlist = str.slice(0,str.length-1);
				var cls:String = plant.cls.toString().split("_")[1];
				cls = cls.substring(0,cls.length-1);
				xxx.@cls =cls.substr(5,cls.length-5);
				xml.appendChild(xxx);
			}
			trace(xml.toString());
		}
		
		public function deleteItem(e:MouseEvent):void{
			isDelete = !isDelete;
		}
		
		protected function editingPlant(event:MouseEvent):void
		{
			if(isDelete){
				ary.splice(ary.indexOf(event.target),1);
				this.removeChild(event.target as DisplayObject);
			}else{
				var plant:PlantItem = event.target as  PlantItem;
				EditPlantPanel.getInstance().show(plant);
			}
		}
		
		public function createPlant(_x:int,_y:int):void{
			if(TowerWarMap.instance.mouseTip.cls){
				var plant:PlantItem = new PlantItem();
				plant.x = _x;
				plant.y = _y;
	            this.addChild(plant);
				plant.cls = TowerWarMap.instance.mouseTip.cls;
				ary.push(plant);
			}
		}
		
		public function initPlantData(xml:XML):void{
			var plant:PlantItem  =new PlantItem();
			this.addChild(plant);
			ary.push(plant);
			plant.x = xml.@x;
		    plant.y = xml.@y;
			plant.hp = xml.@hp;
			plant.energy = xml.@energy;
			var str:String = xml.@gradlist;
			var gradAry:Array = str.split(",");
			var p:Point;
			for(var i:int = 0 ;i<gradAry.length;i++){
				var arr:Array = gradAry[i].split(":");
				p = new Point(arr[0],arr[1]);
				plant.gradAry.push(p);
			}
		    plant.cls = TowerWarMap.instance.plant["plant" + xml.@cls];
			plant.bmp.width = xml.@width;
			plant.bmp.height = xml.@height;
		}

		public function get isCanClick():Boolean
		{
			return _isCanClick;
		}

		public function set isCanClick(value:Boolean):void
		{
			_isCanClick = value;
			if(value){
				this.mouseEnabled = true;
				this.mouseChildren = true;
			}else{
				this.mouseEnabled = false;
				this.mouseChildren = false;
			}
		}

	}
}